import { ethers } from "ethers";
import MintTask from "../Utils/Task.js";
import { PrintLog } from "../Utils/Utils.js";

const MintData =
  "0x646174613a2c7b2270223a227372632d3230222c226f70223a226d696e74222c227469636b223a2273686962222c22616d74223a223130303030303030227d";
const MintRpc = "https://www.shibrpc.com/";
const DiyGasPrice = true; //是否自定义GasPrice
const GasAdd = 30; //自定义GasPrice的增量、在链上的基础上增加多少比如：350+30

const gasLimit = 30000; //自定义GasLimit的值
var GasPriceGwei = 350; //自定义GasPrice的值、可以自己去链上查也可以使用自动Gas单位为Gwei
const MintProvider = new ethers.providers.JsonRpcProvider(MintRpc);
/**
 * 获取当前链上的GasPrice
 */
function GetNowGas() {
  setInterval(async () => {
    const Gas = await MintProvider.getFeeData();
    GasPriceGwei =
      Number(ethers.utils.formatUnits(Gas.gasPrice, "gwei")).toFixed() + GasAdd;
  }, 2000);
}
if (DiyGasPrice) {
  GetNowGas();
}
/**
 *
 * @param {*} AccountObj 自定义的地址操作对象
 * @param {*} index 当前处理到的地址索引
 */
async function Main(AccountObj, index) {
  const Wallet = new ethers.Wallet(AccountObj.privateKey, MintProvider);
  const tx = {
    to: Wallet.address,
    gasPrice: ethers.utils.hexlify(
      ethers.utils.parseUnits(GasPriceGwei.toString(), "gwei")
    ),
    gasLimit: ethers.utils.hexlify(gasLimit),
    data: MintData,
  };
  try {
    const Send = await Wallet.sendTransaction(tx);
    PrintLog(
      `第【${index + 1}】个地址【${Wallet.address}】 交易Hash为：${Send.hash}`
    );
  } catch (error) {
    PrintLog(
      `第【${index + 1}】个地址【${Wallet.address}】 交易失败:${error.reason}`
    );
  }
}
const accounts = [
  {
    privateKey:
      "0x327ba77952a26985954924357fafff42c6d7559519bd5753ad12233875559595",
  },
];
MintTask.startTask(accounts, 1000, 5, Main);
